from db import Dd


class ACL():
    def __init__(self):
        self.db = Dd()

    # 获取权限
    def get_role(self, uid):
        sql = "select node.name,node.auid,node.id from `node_user` inner join `user` on node_user.userid=user.id inner join `node` on node_user.node_id=node.id where user.id=%s" % uid
        list = []
        dict = {}
        data = self.db.get_all(sql)
        print(data)
        for i in data:
            dict['text'] = i['name']
            dict[i['id']] = i
        for j in data:
            auid = j['auid']
            j['text'] = j['name']
            #  pid等于0，就是顶级，
            if auid == 0:
                # 直接加入列表
                list.append(j)
            else:
                #  查看父类是否有子类
                if 'children' not in dict[auid]:
                    dict[auid]['children'] = []
                dict[auid]['children'].append(j)
        print(list)
        return list

    # 添加权限
    def set_role(self, uid, roleid):
        sql = "insert into `node_user` (node_id,userid) values (%s,%s)" % roleid, uid
        self.db.insert(sql)

    # 修改权限
    def update_role(self, uid, roleid):
        sql = "delete  from `node_user` where userid=%s and roleid=%s" % (uid, roleid)
        self.db.delete(sql)
        sql = "insert into `node_user` (node_id,userid) values (%s,%s)" % roleid, uid
        self.db.insert(sql)

    # 删除单个权限
    def del_role(self, uid, roleid):
        sql = "delete  from `node_user` where userid=%s and node_id=%s" % (uid, roleid)
        self.db.delete(sql)


import redis


class RedisCon():
    def __init__(self):
        self.conn = redis.Redis(host='127.0.0.1', port=6379, db=1, decode_responses=True)

    # 字符串存
    def str_set(self, key, value, time=0):
        if time == 0:
            return self.conn.set(key, value)
        else:
            # 设置过期时间，单位是秒
            return self.conn.setex(key, time, value)

    # 字符串读取
    def str_get(self, key):
        value = self.conn.get(key)
        if value:
            value = str(value)
        return value

    # 删除
    def str_del(self, key):
        return self.conn.delete(key)


r = RedisCon()


class RBAC():
    def __init__(self):
        self.db = Dd()

    # 获取所有角色
    def get_all_role(self):
        sql = self.db.get_all("select * from `role`")
        return sql

    # 授权
    def set_role(self, roleid, nodeid):
        sql = "insert into `node_role` (roleid,node_id) values (%s,%s)" % (roleid, nodeid)
        self.db.insert(sql)
        return 'ok'

        # 取消权限

    def del_auth(self, roleid, nodeid):
        self.db.update(" delete from `node_role` where roleid=%s and node_id=%s" % (roleid, nodeid))

    # 获取所有权限
    def get_one(self, uid):
        data = self.db.get_all("select  node.name,node.id from user inner join role on user.roleid =role.id "
                               "inner join node_role on role.id=node_role.roleid "
                               "inner join node on node_role.node_id=node.id where user.id=%s" % uid)
        return data

    # 查询是否有权限
    def get_onderuse(self, uid, nodeid):
        res = self.db.get_all("select  node.name,node.id from user inner join role on user.roleid =role.id "
                              "inner join node_role on role.id=node_role.roleid "
                              "inner join node on node_role.node_id=node.id where user.id=%s and node.id=%s" % (
                                  uid, nodeid))
        if res:
            return True
        else:
            return False

    # 互斥表
    def mepalis_role(self, uid, roleid):
        res = self.db.get_all("select * from `user` where id=%s" % uid)
        roleid = res['roleid']
        data = self.db.get_all('select * from `mutually_role`')
        print(data)
        for i in data:
            if roleid == 1:
                if i['roleid1'] == roleid:
                    print('添加2的角色权限')
            else:
                if i['roleid2'] == roleid:
                    print('添加1的角色权限')


class ABAC:
    def __init__(self):
        self.r = redis.Redis(host='127.0.0.1', port=6379, db=1, decode_responses=True)

    # 添加数据
    def set_insert(self, name, key, value):
        return self.r.hset(name, key, value)

    # 获取相对应的属性
    def get_one(self, name, key):
        return self.r.hget(name, key)

    # 获取所有属性
    def get_all(self, name):
        return self.r.hgetall(name)

    # 删除所有属性
    def del_all(self, name):
        return self.r.delete(name)

    # 删除个别属性属性

    def del_one(self, name, key):
        return self.r.hdel(name, key)
